/*
 *  Initiative.cpp
 *  initiativeTracker
 *
 *  Created by Sam Jaffe on 5/6/12.
 *  Copyright 2012 WPI. All rights reserved.
 *
 */

#include <string>
#include <algorithm>
#include <ctype.h>
#include <iostream>
#include <regex>
#include <typeinfo>
#include "Initiative.h"
#include "reader.h"

const std::regex yes_reg("([yY](|[eE][sS]))|[tT][rR][uU][eE]");
const std::regex exp_reg("[nN][eE][wW]");

void reroll(InitTracker* init) {
    std::string inputLine;
    //Check turn data
    if (init->getNext() > 0 || init->getTurn() > 1) {
        std::cout << "Reset Turns: ";
        inputLine = readLine();
        if (regex_match(inputLine, yes_reg))
            init->reset(); //Reset turns
    }
    bool exp = false;
    
    std::vector<std::shared_ptr<Character>>::iterator it;
    for (it = init->begin(); it < init->end(); it++) {
        std::shared_ptr<Character> c = *it;
        std::string type(typeid(*c).name());
        if (type.compare(pc_type)) {
            std::cout << "Is " << c->getName() << " continuing: ";
            inputLine = readLine();
            if (regex_match(inputLine, yes_reg));
            else if (regex_match(inputLine, exp_reg)) {
                exp = true;
                break;
            }
            else
                return;
        }
    }
    newRoll(init, exp);
    //Changeturn
}

int rollInit(std::shared_ptr<Character> c) {
    int input = readInt("Initiative Roll for " + c->getName());
    c->setInit(static_cast<short>(input));
    return input;
}

void newRoll(InitTracker* init, bool exp) {
    std::cout << "Please reroll the Initiative for all characters...\n";
    std::cout << "Is this an extended rest: ";
    std::string s = readLine();
    bool rest = false;
    
    if (regex_match(s, yes_reg))
        rest = true;
    
    std::vector<std::shared_ptr<Character>>::iterator it;
    for (it=init->begin(); it < init->end(); it++)
        checkRoll(*it, rest, exp);
    
    std::sort(init->begin(), init->end(), compare);
}

void checkRoll(std::shared_ptr<Character> c, bool rest, bool exp) {
    rollInit(c);
    std::string type(typeid(*c).name());
    if (type.compare(pc_type)) {
        PC* temp_pc = static_pointer_cast(c);
        temp_pc->nullTemp();
        
        unsigned short surgeLeft=temp_pc->getSurgeLeft();
        unsigned short surgeMax=temp_pc->getSurgeMax();
        unsigned short surgeVal=temp_pc->getSurgevalue();
        
        if (rest) {
            temp_pc->setSurgeLeft(surgeMax);
            c->setHp(c->getHp_max());
        }
        else if (exp) {
            std::cout << "How many healing surges" <<
                " would you like to use?\n";
            
            std::cout << "HP = " << c->getHp() << "/" << c->getHp_max() << std::endl;
            
            std::cout << "You have " << surgeLeft <<
            " Surges left at " << surgeVal <<
            " HP per surge.\n";
            
            short needed = (c->getHp_max()-c->getHp())/surgeVal;
            if (needed > surgeLeft) {
                needed = surgeLeft;
                std::cout << "The maximum number of surges you" << " can use is " << needed << std::endl;
            }
            else
                std::cout << "The maximum number of surges needed is " << needed << std::endl;
            
            short i = static_cast<short> (
                readInt("Please enter the number of surges to use"));
            
            if (i > needed)
                i = needed;
            while (i-->0) {
                temp_pc->surge(0);
            }
        }
    }
}

bool compare(std::shared_ptr<Character> c1, std::shared_ptr<Character> c2) {
    int diff = c1->getInit() - c2->getInit();
    return diff < 0 || (!diff &&
                        (c1->getName().compare(c2->getName())) < 0);
}

